package com.ruoyi.system.interfacechannel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.V2.gameAttr.service.IGameAttrTemplateService;
import com.ruoyi.system.interfacechannel.domain.InterfacePush;
import com.ruoyi.system.interfacechannel.mapper.InterfacePushMapper;
import com.ruoyi.system.interfacechannel.service.IInterfacePushService;
import com.ruoyi.system.model.domain.GameAttrTemplate;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueReq;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 接口推送表(interface_push)表服务实现类
 *
 * @author zyy
 * @date 2024-01-22 14:10:01
 */
@Service("interfacePushService")
public class InterfacePushServiceImpl extends ServiceImpl<InterfacePushMapper, InterfacePush> implements IInterfacePushService {

    @Autowired
    private IGameAttrTemplateService gameAttrTemplateService;
    @Autowired
    private ISysUserService sysUserService;

    @Override
    public boolean handleAndSaveBatch(List<InterfacePush> interfacePushes) {
        SysUser sysUser = sysUserService.selectUserById(SecurityUtils.getLoginUser().getUserId());
        for (InterfacePush interfacePush : interfacePushes) {
            interfacePush.setScreenValue(setScreenValue(interfacePush.getContent()));
            interfacePush.setCreateId(sysUser.getUserId());
            interfacePush.setCreateName(sysUser.getNickName());
        }
        saveBatch(interfacePushes);
        return true;
    }

    @Override
    public String setScreenValue(String content) {
        String res = null;
        if (StringUtils.isNotEmpty(content)) {
            GoodsInfoAndAttrValueReq goodsInfoAndAttrValueReq = null;
            try {
                goodsInfoAndAttrValueReq = JSONObject.parseObject(content,
                        GoodsInfoAndAttrValueReq.class);
            } catch (Exception e) {

            }
            if (Objects.isNull(goodsInfoAndAttrValueReq)) {
                throw new GlobalException("筛选条件解析出错");
            }
            res = getScreenValueSql(goodsInfoAndAttrValueReq);
        }
        return res;
    }

    String getScreenValueSql(GoodsInfoAndAttrValueReq goodsInfoAndAttrValueReq) {

        Field[] fields = goodsInfoAndAttrValueReq.getClass().getDeclaredFields();
        Map map1 = new HashMap();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.getName().startsWith("f")) {
                    map1.put(field.getName(), field.get(goodsInfoAndAttrValueReq));
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        StringBuffer sbuffer = new StringBuffer();
        List<Long> systemIdList = goodsInfoAndAttrValueReq.getSystemIdList();
        if (CollectionUtils.isNotEmpty(systemIdList)) {
            sbuffer.append("and goods.system_id in (");
            for (Long s : systemIdList) {
                sbuffer.append(s + ",");
            }
            sbuffer = new StringBuffer(sbuffer.substring(0, sbuffer.length() - 1));
            sbuffer.append(") ");
        }
        List<Long> groupIdList = goodsInfoAndAttrValueReq.getGroupIdList();
        if (CollectionUtils.isNotEmpty(groupIdList)) {
            sbuffer.append("and goods.group_id in (");
            for (Long s : groupIdList) {
                sbuffer.append(s + ",");
            }
            sbuffer = new StringBuffer(sbuffer.substring(0, sbuffer.length() - 1));
            sbuffer.append(") ");
        }
        List<Long> carrierIdList = goodsInfoAndAttrValueReq.getCarrierIdList();
        if (CollectionUtils.isNotEmpty(carrierIdList)) {
            sbuffer.append("and goods.carrier_id in (");
            for (Long s : carrierIdList) {
                sbuffer.append(s + ",");
            }
            sbuffer = new StringBuffer(sbuffer.substring(0, sbuffer.length() - 1));
            sbuffer.append(") ");
        }
        if (StringUtils.isNotEmpty(goodsInfoAndAttrValueReq.getPriceStart())) {
            sbuffer.append("and goods.price >= " + goodsInfoAndAttrValueReq.getPriceStart() + " ");
        }
        if (StringUtils.isNotEmpty(goodsInfoAndAttrValueReq.getPriceEnd())) {
            sbuffer.append("and goods.price <= " + goodsInfoAndAttrValueReq.getPriceEnd() + " ");
        }
        if (StringUtils.isNotNull(goodsInfoAndAttrValueReq.getGameId())) {
            List<GameAttrTemplate> gameAttrTemplates =
                    gameAttrTemplateService.selectGameAttrTemplateByGameId(goodsInfoAndAttrValueReq.getGameId());
            if (CollectionUtils.isNotEmpty(gameAttrTemplates)) {
                for (GameAttrTemplate gameAttrTemplate : gameAttrTemplates) {
                    Object aa = map1.get(gameAttrTemplate.getAavName());
                    pinjie(aa, gameAttrTemplate, sbuffer);
                }
                List list = goodsInfoAndAttrValueReq.getBoxRadioValues();
                if (CollectionUtils.isNotEmpty(list)) {
                    for (Object o : list) {
                        String value = o.toString();
                        if (StringUtils.contains(value, "===")) {
                            String[] vlas = value.split("===");
                            for (int i = 0; i < vlas.length; i++) {
                                if (i == 0) {
                                    sbuffer.append("and ( all_field like \'%" + vlas[i] + "%\'");
                                } else {
                                    if (i == vlas.length - 1) {
                                        sbuffer.append("or all_field like \'%" + vlas[i] + "%\' ) ");
                                    } else {
                                        sbuffer.append("or all_field like \'%" + vlas[i] + "%\'");
                                    }
                                }
                            }
                        } else {
                            sbuffer.append(" and all_field like \'%" + value + "%\'");
                        }
                    }
                }
//                goodsInfoAndAttrValueReq.setRemark(sbuffer.toString());
            }
        }
        String[] keyWordArr = {};
        if (StringUtils.isNotEmpty(goodsInfoAndAttrValueReq.getKeyWord())) {
            keyWordArr = goodsInfoAndAttrValueReq.getKeyWord().split(" ");
            if (keyWordArr != null && keyWordArr.length > 0) {
                for (String s : keyWordArr) {
                    sbuffer.append("and (goods.all_field like '" + s + "' or goods.des like '" + s + "') ");
                }
            }
        }
        return sbuffer.toString();
    }

    String pinjie(Object autoF, GameAttrTemplate gameAttrTemplate, StringBuffer sbuffer) {
        if (Objects.nonNull(autoF)) {
            String vlaues = autoF.toString();
            if (StringUtils.isNotEmpty(vlaues)) {
                if (StringUtils.equals(gameAttrTemplate.getFieldType(), "input")) {
                    sbuffer.append(" and " + gameAttrTemplate.getAavName() + " like \'%" + autoF + "%\'");
                } else if (StringUtils.equals(gameAttrTemplate.getFieldType(), "number") || StringUtils.equals(gameAttrTemplate.getFieldType(), "date")) {
                    if (vlaues.contains("===")) {
                        String[] vlas = vlaues.split("===");
                        if (vlas.length == 2) {
                            if (StringUtils.isNotEmpty(vlas[0])) {
                                sbuffer.append(" and " + gameAttrTemplate.getAavName() + " >= " + vlas[0]);
                            }
                            sbuffer.append(" and " + gameAttrTemplate.getAavName() + " <= " + vlas[1]);
                        } else if (vlas.length == 1) {
                            sbuffer.append(" and " + gameAttrTemplate.getAavName() + " >= " + vlas[0]);
                        }
                    } else {
                        sbuffer.append(" and " + gameAttrTemplate.getAavName() + " >= " + vlaues);
                    }
                } else if (StringUtils.equals(gameAttrTemplate.getFieldType(), "radio")) {
                    sbuffer.append(" and " + gameAttrTemplate.getAavName() + " = " + vlaues);
                } else if (StringUtils.equals(gameAttrTemplate.getFieldType(), "checkbox")) {
                    if (vlaues.contains(",")) {
                        String[] vlas = vlaues.split(",");
                        for (String vla : vlas) {
                            sbuffer.append(" and FIND_IN_SET(\"" + vla + "\"," + gameAttrTemplate.getAavName() + ") >" +
                                    " 0 ");
                        }
                    } else {
                        sbuffer.append(" and FIND_IN_SET(\"" + vlaues + "\"," + gameAttrTemplate.getAavName() + ") >" +
                                " 0 ");
                    }
                }
            }
        }
        return sbuffer.toString();
    }
}

